//
//  GRPayManager.m
//  YC_GRice
//
//  Created by LiuFeng on 2018/7/10.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRPayManager.h"
#import "GRBarginViewController.h"
@interface GRPayManager () <WXApiDelegate>

@end

@implementation GRPayManager

+ (instancetype)sharePay {
    static GRPayManager *pay = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        pay = [[GRPayManager alloc] init];
    });
    
    return pay;
}

+ (void)PaySDK {
    [WXApi registerApp:WECHATKEY];
}


#pragma Mark ==============    微信     =================
+ (void)WeChatPay:(NSDictionary *)option {
    PayReq *resp   = [[PayReq alloc] init];
    
    resp.openID    = option[@"appid"];
    resp.partnerId = option[@"partnerid"];
    resp.prepayId  = option[@"prepayid"];
    resp.nonceStr  = option[@"noncestr"];
    resp.timeStamp = [option[@"timestamp"] intValue];
    resp.package   = option[@"package"];
    resp.sign      = option[@"sign"];
    
    [WXApi sendReq:resp];
}


+ (void)WechatAuth_WithViewController:(UIViewController *)currentVc completion:(void(^)(UMSocialUserInfoResponse *response))completion {
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:currentVc completion:^(id result, NSError *error) {
        if (!error) {
            completion(result);
        }else {
            NSLog(@"error = %@",error);
        }
    }];
}


#pragma Mark ==============    支付宝     =================

- (void)aliPay:(NSString *)orderStr {
    [[AlipaySDK defaultService] payOrder:orderStr fromScheme:ALIPAYSCHEME callback:^(NSDictionary *resultDic) {
        if ([self.delegate respondsToSelector:@selector(alipayResultDic:)]) {
            [self.delegate alipayResultDic:resultDic];
        }
    }];
}

+(void)AlipayAuth_V2WithInfo:(NSString *)infoStr completion:(CompletionBlock)completion {
    [[AlipaySDK defaultService] auth_V2WithInfo:infoStr fromScheme:ALIPAYSCHEME callback:^(NSDictionary *resultDic) {
        completion(resultDic);
    }];
}


- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url {
    if ([url.host isEqual:@"safepay"]) {
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            if ([self.delegate respondsToSelector:@selector(alipayResultDic:)]) {
                [self.delegate alipayResultDic:resultDic];
            }
        }];
        
        [[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
            if ([self.delegate respondsToSelector:@selector(alipayResultDic:)]) {
                [self.delegate alipayResultDic:resultDic];
            }
        }];
        return YES;
    }else {
        BOOL uMesult = [[UMSocialManager defaultManager] handleOpenURL:url];
        if (uMesult) {
            return uMesult;
        }else {
            if ([url.scheme isEqualToString:@"tortoiserice"]) {
                [self griceBargining:url.absoluteString
                 ];
                return YES;
            }else{
                return [WXApi handleOpenURL:url delegate:self];
            }
        }
    }
}


-(void)griceBargining:(NSString *)url{
        NSString *hggleId = [url stringByReplacingOccurrencesOfString:@"tortoiserice://bargain.com?id=" withString:@""];
    
    GRBarginViewController * barginVC = [[GRBarginViewController alloc]init];
    barginVC.barginId = [hggleId integerValue];
    
        UIWindow * window =  [UIApplication sharedApplication].keyWindow;
    [window.rootViewController presentViewController:barginVC completion:nil];
    
    
}


-(void)onResp:(BaseResp *)resp {
    if ([self.delegate respondsToSelector:@selector(wechatPayResp:)]) {
        [self.delegate wechatPayResp:resp];
    }
}


@end
